
import 'dart:async';
import 'dart:developer';

import 'package:logging/logging.dart';
import 'CYDateFormat.dart';

class CYLoggerData {
  /// 创建时间
  DateTime createAt;
  /// 所属模块
  final String tag;
  /// 日志等级
  final String level;
  /// 消息
  final String message;

  CYLoggerData({
    this.tag, 
    this.level, 
    this.message
  }) {
    this.createAt = DateTime.now().toUtc();
  }
}

class CYLogger {

  static StreamController<CYLoggerData> prints = StreamController<CYLoggerData>.broadcast();
  static bool isDev = false;

  CYLogger({ this.tag = "CYLogger" });

  String tag;
  
  _log(String level, String message, [
    int logLevel
  ]) async {
    if(isDev) {
      String now = CYDateFormat.toLocalStringFromUtc(DateTime.now().toUtc());
      if(logLevel != null)
        log("[$tag][$now]$message", level:  logLevel);
      else 
        print("[$tag][$now]$message");
    }
    prints.add(
      CYLoggerData(tag: tag, level: level, message: message)
    );
  }

  error(Object message) {
    _log("error", "$message", Level.SEVERE.value);
  }

  warning(Object message) {
    _log("warning", "$message", Level.WARNING.value);
  }

  debug(Object message) {
    _log("debug", "$message");
  }

  info(Object message) {
    _log("info", "$message");
  }
}